package com.efast.cafe.portal.controller.common;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.constant.CommonConstant;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.redis.ObjectRedisTemplate;
import com.efast.cafe.framework.util.SpringWebUtils;

/**
 * 获取某个菜单下拥有的权限
 * @author fxm
 *
 */
@Controller
public class QueryPurviewByMenuController  extends AbstractController{

	private ObjectRedisTemplate objectRedisTemplate;
	
	@Autowired
	public void setObjectRedisTemplate(ObjectRedisTemplate objectRedisTemplate) {
		this.objectRedisTemplate = objectRedisTemplate;
	}
	
	@RequestMapping(value = "/frame_center/common/purviewsByMenu")
	public @ResponseBody ResultBean initCompanyRestDays(@RequestParam(required=false) String menuId){
		try{ ResultBean result = new ResultBean(true,"");
			if(StringUtils.isNotBlank(menuId)){
				ValueOperations<String,Object> redisValueObj = objectRedisTemplate.opsForValue();
				String username = SpringWebUtils.getRequestUser();
				String companyCode = SpringWebUtils.getRequestCompany();
				Map<String, List<String>> map = (Map<String, List<String>>)redisValueObj.get(CommonConstant.REDIS_COMPANYUSER_PURVIEWSET_KEY_BY_MENU+companyCode+username);
				
				if(null!=map){
					List<String> res = map.get(menuId);
					result.setObject(res);
					if(null==res || res.isEmpty()){
						res = map.get(menuId.replaceFirst("/",""));
						result.setObject(res);
					}
				}else{
					//result.setSuccess(false);
				}
			}
			return result;
		}catch(Exception e){
			e.printStackTrace();
			return new ResultBean(false, ResultStatus.SYS_ERROR.getCode());
		}
	}
}
